<?php

/**
* baseService .php
*
* service积累
*
* @author witiq <微信号：937731903>
* @version 1.0
* @package service
*/

class  baseService extends Service {

	/**
	 *@var ownDao
	 */
	protected $ownDao;

	public function __construct(){
		parent::__construct();
	}

	/**
	 *  获取对应表的主键
	 * @return [type] [description]
	 */
	public function getTableKey(){
		return $this->ownDao->table_key;
	}

	/**
	 * 创建一个模型
	 * @param $model
	 */
	public function createModel($model){
	    $r = $this->validateData($model);
		if(isset($r['error'])){
			return $r;
		}
		return $this->ownDao->create($model);
	}

	/**
	 * 	创建多个模型
	 *	@param $fields [字段数组] array('field1','field2',...)
	 *	@param $model [值数组]	array(array('val1','val2'),array('val1','val2'),...)
	 *	@return $ids 
	 */
	public function createMoreModel($fields,$model){
		return $this->ownDao->createMore($fields,$model);
	}


	/**
	 * 更新一个模型
	 * @param $user
	 */
	public function updateModel($model){
		//数据验证
		$r = $this->validateData($model,true);
		if(isset($r['error'])){
			return $r;
		}
		return $this->ownDao->update($model);
	}
	
	/**
	 * [updateByFieldsModel 根据条件更新模型]
	 * @param  [type] $model  [description]
	 * @param  [type] $fields [description]
	 * @return [type]         [description]
	 */
 	public function updateModelByFields($model,$fields){
 		$r = $this->validateData($model,true);
		if(isset($r['error'])){
			return $r;
		}
 		return $this->ownDao->updateByFields($model,$fields);
 	}


	/**
	 * [deleteModel 删除一个或多个数据]
	 * @param  [type] $ids [description]
	 * @return [type]      [description]
	 */
	public function deleteModel($ids){
		$ids = (array) $ids;		
		return $this->ownDao->delete($ids);
	}


	/**
	 * 	[count 查询数据条数]
	 * 	@param  $fields [查询条件数组] array('key1'=>'val1','key2'=>'val2')
	 * 	@return int 
	 */
	public function getCount($fields){
		return $this->ownDao->count($fields);
	}

	/**
	 * 根据主键获取一个模型
	 *@param $id
	 */
	public function getModel($id,$fields=array()){
		if($id < 1 ){
			return array();
		}
		$model = $this->ownDao->findOneById($id);
		return $model;
	}

	/**
	 * [getModelByFields 根据条件获取一条数据]
	 * @return [type] [description]
	 */
	public function getModelByFields($fields){
		return $this->ownDao->findOneByFields($fields);
	}


	

	/**
	 *	获取列表页面
	 *	@param $fields 条件数组支持复杂类型
	 *	@param $page 
	 *	@param $pagesize
	 *	@param $selectfields 要查询的字段
	 *	@param $orderkey
	 *	@param $sort
	 * 	@return array
	 */
	public function getModelList($fields,$page='',$pagesize='',$selectfields='',$orderkey='',$sort='DESC'){
		return   $this->ownDao->findList($fields,$page='',$pagesize='',$selectfields='',$orderkey='',$sort='DESC');
	}

	/**
	 *	查找个数
	 *	@param $fields 条件数组支持复杂类型
	 * 	@return int
	 */
	public function getListNum($fields){
		return $this->ownDao->findListNum($fields);
	}

	/**
	 *	查找键值对
	 *	@param $k_filed 键key
	 *	@param $v_filed 值key
	 *	@param $field 条件数组支持复杂类型
	 *	@param $page
	 *	@param $pagesize
	 *	@return 键值对
	 */
	public function getKV($k_filed, $v_filed,$fields=array(),$page='',$pagesize=''){
		return $this->ownDao->findKV($k_filed, $v_filed,$fields=array(),$page='',$pagesize='');
	}


	/**
	 * [getModelQuery 特殊sql条件查询]
	 * @param  [type] $query    [description]
	 * @param  [type] $fields   [description]
	 * @param  string $orderkey [description]
	 * @param  string $sort     [description]
	 * @return [type]           [description]
	 */
	public function getModelQuery($query,$fields,$orderkey='',$sort='DESC'){

		return $this->ownDao->findQuery($query,$fields,$orderkey,$sort);

	}

	/**
	 *获getTreePage
	 *@param $field 条件数组支持复杂类型
	 *@param $page      1 
	 *@param $pagesize  2 
	 *@param $orderkey
	 *@param $sort
	 */
	public function getTreePage($fields){	
		$res =  $this->ownDao->findList($fields,'','','',$this->tree_fields['order'],'ASC');		
		$tree = InitPHP::getLibrary('tree');
	    $tree->init($this->tree_fields);
	    return $tree->get_tree_list($res);
	}

	/**
	 * [getTree description]
	 * @param  integer $mid      [description]
	 * @param  integer $selected [description]
	 * @return [type]            [description]
	 */
	public function getTree($mid = 0 ,$selected = 0,$fields=array()){
		$list =  $this->ownDao->findList($fields,'','','',$this->tree_fields['order'],'ASC');	
   		$tree = InitPHP::getLibrary('tree');
	    $tree->init($this->tree_fields);
	    return $tree->get_tree($list,$mid,$selected);
	}


	/**
	 *  getTreeArray
	 */
	public function getTreeArray($root = 0,$mid="",$pid = "",$child = "childNode"){

		if($mid ==''){
			$mid = $this->tree_fields['id'];
		}
		if($pid ==''){
			$pid = $this->tree_fields['parentid'];
		}
		// 创建Tree  
		$tree = array();  
		$list =  $this->ownDao->findList(array(),'','','',$this->tree_fields['order'],'ASC');
		if(is_array($list)) {  
	        // 创建基于主键的数组引用  
	        $refer = array();  
	        foreach ($list as $key => $data) {  
	            $refer[$data[$mid]] =& $list[$key];  
	        }  
	        foreach ($list as $key => $data) {  
	            // 判断是否存在parent  
	            $parentId =  $data[$pid];  
	            if ($root == $parentId) {  
	                $tree[] =& $list[$key];  
	            }else{  
	                if (isset($refer[$parentId])) {  
	                    $parent =& $refer[$parentId];  
	                    $parent[$child][] =& $list[$key];  
	                }  
	            }  
	        }  
	    }  
	    return $tree;
	}
	
}